import { Vector } from '../../engine/common/vector';
import { IWorld } from '../../engine/ecs/common/world.interface';
import { CollisionEventComponent } from '../../engine/ecs/components/collision-event.component';
import { LocationComponent } from '../../engine/ecs/components/location.component';
import { System } from '../../engine/ecs/systems/system';
import { BulletComponent } from '../components/bullet.component';
import { ExplositionFactory } from '../factories/explosition-factory';

export class BulletCollisionSystem extends System {
  constructor(private explosionSprite: HTMLImageElement) {
    super();
  }

  onUpdate(world: IWorld, dt: number) {
    const bulletEntities = world.findEntitiesByComponents([
      BulletComponent,
      LocationComponent,
      CollisionEventComponent,
    ]);
    for (const bulletEntity of bulletEntities) {
      const locationComponent = world.findComponentByEntity(bulletEntity, LocationComponent)!;
      ExplositionFactory.create(world, locationComponent.location.substract(new Vector(25, 25)), this.explosionSprite);
      world.removeEntity(bulletEntity);
    }
  }
}
